<?php

namespace App\Models;

use App\Models\Scopes\ActiveScope;
use Illuminate\Database\Eloquent\Model;
use Overtrue\Pinyin\Pinyin;

class Post extends Model
{
    protected $fillable = [
        'title',
        'slug',
        'description',
        'picture',
        'content',
        'show',
        'seo_keywords',
        'seo_description',
    ];

    protected $casts = [
        'show' => 'bool',
    ];

    protected $attributes = [
        'show' => true,
    ];

    protected static function booted(): void
    {
        static::addGlobalScope(new ActiveScope);

        static::saving(function (Post $post) {
            if ($post->isDirty('title')) {
                $post->slug = Pinyin::permalink($post->title);
            }
        });
    }
}
